package com.briup.bookstore.service.impl;

import com.briup.bookstore.mapper.CollectMapper;
import com.briup.bookstore.po.Collect;
import com.briup.bookstore.po.ext.CollectExtend;
import com.briup.bookstore.service.CollectService;
import com.briup.bookstore.utils.BeanCopyUtils;
import com.briup.bookstore.vo.CollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Override
    public List<CollectVO> findCollects(Integer userId) {
        List<CollectExtend> collects = collectMapper.selectListByUserId(userId);
        List<CollectVO> collectVOS = BeanCopyUtils.copyBeanList(collects, CollectVO.class);

        return collectVOS;
    }

    @Override
    public void addCollect(Integer userId, Integer bookId) {
        // 先判断是否收藏过
        Collect collect = collectMapper.selectByBookIdAndUserId(bookId, userId);
        // 如果没有再加入收藏
        if(Objects.isNull(collect)){
            collectMapper.insert(userId, bookId);
        }
    }

    @Override
    public void deleteCollect(Integer... id) {
        List<Integer> ids = Arrays.asList(id);
        collectMapper.delete(ids);
    }
}
